package com.meis.widget.praise;

import ohos.agp.utils.Point;

/**
 * Created by wenshi on 2018/7/10.
 * Description 点赞估值器
 */
public class PraiseEvaluator {

    // 贝塞尔曲线的控制点
    private Point controlF1;
    private Point controlF2;
    private Point result = new Point();

    public PraiseEvaluator(Point controlF1) {
        this.controlF1 = controlF1;
    }

    public PraiseEvaluator(Point controlF1, Point controlF2) {
        this.controlF1 = controlF1;
        this.controlF2 = controlF2;
    }

    public Point evaluate(float time, Point startValue, Point endValue) {
        //估值
        float currentX;
        float currentY;
        if (controlF2 == null) {
            // 二阶贝塞尔曲线
            currentX = arithmeticProduct(1 - time, 2) * startValue.getPointX()
                    + 2 * time * (1 - time) * controlF1.getPointX()
                    + arithmeticProduct(time, 2) * endValue.getPointX();
            currentY = arithmeticProduct(1 - time, 2) * startValue.getPointY()
                    + 2 * time * (1 - time) * controlF1.getPointY()
                    + arithmeticProduct(time, 2) * endValue.getPointY();
        } else {
            // 三阶贝塞尔曲线
            currentX = arithmeticProduct(1 - time, 3) * (startValue.getPointX())
                    + 3 * arithmeticProduct(1 - time, 2) * time * (controlF1.getPointX())
                    + 3 * (1 - time) * arithmeticProduct(time, 2) * (controlF2.getPointX())
                    + arithmeticProduct(time, 3) * (endValue.getPointX());
            currentY = arithmeticProduct(1 - time, 3) * (startValue.getPointY())
                    + 3 * arithmeticProduct(1 - time, 2) * time * (controlF1.getPointY())
                    + 3 * (1 - time) * arithmeticProduct(time, 2) * (controlF2.getPointY())
                    + arithmeticProduct(time, 3) * (endValue.getPointY());
        }
        result.modify(currentX, currentY);
        return result;
    }

    // 返回浮点数的开方值
    private float arithmeticProduct(float value, float square) {
        double pow = Math.pow(value, square);
        return (float) pow;
    }
}
